home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / sspell14.zip / SSPELL.C < prev    next >
C/C++ Source or Header  |  1992-07-18  |  9KB  |  348 lines

  1. /* **************************************************************** */
  2. /*             sspell - similar to Unix spell                       */
  3. /*                        version 1.4                               */
  4. /*                                                                  */
  5. /* Author: Maurice Castro                                           */
  6. /* Release Date:  4 Jul 1992                                         */
  7. /* Bug Reports: maurice@bruce.cs.monash.edu.au                      */
  8. /*                                                                  */
  9. /* This code has been placed by the Author into the Public Domain.  */
  10. /* The code is NOT covered by any warranty, the user of the code is */
  11. /* solely responsible for determining the fitness of the program    */
  12. /* for their purpose. No liability is accepted by the author for    */
  13. /* the direct or indirect losses incurred through the use of this   */
  14. /* program.                                                         */
  15. /*                                                                  */
  16. /* Segments of this code may be used for any purpose that the user  */
  17. /* deems appropriate. It would be polite to acknowledge the source  */
  18. /* of the code. If you modify the code and redistribute it please   */
  19. /* include a message indicating your changes and how users may      */
  20. /* contact you for support.                                         */
  21. /*                                                                  */
  22. /* The author reserves the right to issue the official version of   */
  23. /* this program. If you have useful suggestions or changes for the  */
  24. /* code, please forward them to the author so that they might be    */
  25. /* incorporated into the official version                           */
  26. /*                                                                  */
  27. /* Please forward bug reports to the author via Internet.           */
  28. /*                                                                  */
  29. /* **************************************************************** */
  30.  
  31. #include "cache.h"
  32. #include "file.h"
  33. #include <stdio.h>
  34. #include "strfn.h"
  35. #include "config.h"
  36. #include "check.h"
  37. #include <signal.h>
  38. #ifdef __TURBOC__
  39. #include <dir.h>
  40. #endif
  41. #include "error.h"
  42. #include "stop.h"
  43. #if !defined(pyr)
  44. #include <stdlib.h>
  45. #endif
  46.  
  47. extern void cfgload();
  48.  
  49. /* fix up some slash problems with MS-DOS */
  50. #ifdef __MSDOS__
  51. extern void dosfix();
  52. #endif
  53.  
  54. FILE *fout;
  55. int prtderive;
  56. int prtroot;
  57. char progname[MAXSTR];
  58. char ftmp1[MAXSTR];
  59. char ftmp2[MAXSTR];
  60.  
  61. char dictpath[MAXSTR];
  62. char rule[MAXSTR];
  63. char indexarr[MAXSTR];
  64. char dictionary[MAXSTR];
  65. char sort[MAXSTR];
  66. char stop[MAXSTR];
  67.  
  68. void emergencyshutdown();
  69.  
  70.  
  71. void errormesg(mesg,val)
  72. char *mesg;
  73. int val;
  74. {
  75.    fprintf(stderr,"%s: %s\n",progname, mesg);
  76.    exit(val);
  77.    }
  78.  
  79. void usage()
  80. {
  81.    fprintf(stderr,"%s: Usage:\n",progname);
  82.    fprintf(stderr,"%s [-u] [-v] [-x] [-c config] [-D dict] [-I index] [-R rule]\n\t[-C cachesize] [-S stop] [file] ...\n",progname);
  83.    emergencyshutdown();
  84.    exit(1);
  85.    }
  86.  
  87. void emergencyshutdown()
  88. {
  89.    FILE *f;
  90.    if (fout != NULL) fclose(fout);
  91.    f = fopen(ftmp1,"rt");
  92.    if (f != NULL)
  93.    {
  94.       fclose(f);
  95.       unlink(ftmp1);
  96.       }
  97.    f = fopen(ftmp2,"rt");
  98.    if (f != NULL)
  99.    {
  100.       fclose(f);
  101.       unlink(ftmp2);
  102.       }
  103.    exit(-10);
  104.    }
  105.  
  106. void main(argc, argv)
  107. int argc;
  108. char *argv[];
  109. {
  110.    char ssys[MAXSTR];
  111.    char tstr1[MAXSTR];
  112.    char tstr2[MAXSTR];
  113.    char cfgname[MAXSTR];
  114.    int cachesize;
  115.    int pt;
  116.    FILE *f;
  117.    char *tmpdir;
  118.    char *dictdir;
  119.    int sortout = 1;
  120.  
  121.    /* setup */
  122.    strcpy(ftmp1,"");
  123.    strcpy(ftmp2,"");
  124. #if defined(pyr) || defined(__TURBOC__)
  125.    signal(SIGABRT, emergencyshutdown);
  126. #endif
  127.    signal(SIGINT, emergencyshutdown);
  128.    signal(SIGTERM, emergencyshutdown); 
  129. #ifdef __TURBOC__
  130.    atexit(emergencyshutdown);
  131. #endif
  132.    prtderive = 0;
  133.    prtroot = 0;
  134.    strcpy(progname,argv[0]);
  135.  
  136.    /* make the temporary file name */
  137.    tmpdir = getenv("TMP");        /* retrieve path from environment */
  138.    if (tmpdir == NULL)            /* Suggested Mike O'Carroll */
  139.     tmpdir = getenv("TEMP");    /* M. Castro 2/7/92 */
  140.  
  141.    if (tmpdir == NULL)
  142.        strcpy(ftmp1, "");
  143.    else
  144.    {
  145.        strcpy(ftmp1, tmpdir);
  146. #ifdef __MSDOS__
  147.     dosfix(ftmp1);
  148. #endif
  149.     if (strcmp(ftmp1,""))
  150.         if (strcmp(ftmp1+strlen(ftmp1)-strlen(SEPARATOR), SEPARATOR))
  151.         {                    /* add a separator */
  152.         strcat(ftmp1, SEPARATOR);
  153.             }
  154.     }
  155.  
  156.    strcat(ftmp1, ROOTNAME);
  157.    strcpy(ftmp2, ftmp1);
  158.  
  159.    strcat(ftmp1,"1");
  160.    strcat(ftmp1,"XXXXXX");
  161.    mktemp(ftmp1);
  162.    strcat(ftmp2,"2");
  163.    strcat(ftmp2,"XXXXXX");
  164.    mktemp(ftmp2);
  165.    fout = fopen(ftmp1,"wt");
  166.    if (fout == NULL)
  167.    {
  168.        errormesg("Unable to open temporary file. Aborting",-1);
  169.        }
  170.  
  171.    /* startup */
  172.    dictdir = getenv("SSPELL");        /* retrieve path from environment */
  173.    if (dictdir == NULL)            /* Suggested Mike O'Carroll */
  174.     strcpy(dictpath, DICT_PATH);     /* M. Castro 2/7/92 */
  175.    else
  176.     strcpy(dictpath, dictdir);
  177.  
  178. #ifdef __MSDOS__
  179.    dosfix(dictpath);
  180. #endif
  181.    /* if there is a path, and it does not end in a separator make it */
  182.    if (strcmp(dictpath,""))
  183.       if (strcmp(dictpath+strlen(dictpath)-strlen(SEPARATOR), SEPARATOR))
  184.          strcat(dictpath, SEPARATOR);     /* add a separator */
  185.  
  186.    rule[0] = NULL;
  187.    indexarr[0] = NULL;
  188.    dictionary[0] = NULL;
  189.    sort[0] = NULL;
  190.    stop[0] = NULL;
  191.    
  192.    strcpy(cfgname, dictpath);
  193.    strcat(cfgname, CFGNAME);
  194.    cfgload(cfgname);
  195.  
  196.    cachesize = CACHESIZE;
  197.  
  198.    pt = 1;
  199.    while ((pt < argc) && (*(argv[pt]) == '-'))
  200.    {
  201.        if (!strcmp(argv[pt], "-c"))
  202.        {
  203.           pt++;
  204.           if (pt == argc) usage();
  205.           strcpy(cfgname,argv[pt]);
  206.           cfgload(cfgname);
  207.           pt++;
  208.           continue;
  209.           }
  210.        if (!strcmp(argv[pt], "-v"))
  211.        {
  212.           prtderive = 1;
  213.           pt++;
  214.           continue;
  215.           }
  216.        if (!strcmp(argv[pt], "-u"))
  217.        {
  218.           sortout = 0;
  219.           pt++;
  220.           continue;
  221.           }
  222.        if (!strcmp(argv[pt], "-x"))
  223.        {
  224.           prtroot = 1;
  225.           pt++;
  226.           continue;
  227.           }
  228.        if (!strcmp(argv[pt], "-D"))
  229.        {
  230.           pt++;
  231.           if (pt == argc) usage();
  232.           strcpy(dictionary,argv[pt]);
  233.           pt++;
  234.           continue;
  235.           }
  236.        if (!strcmp(argv[pt], "-R"))
  237.        {
  238.           pt++;
  239.           if (pt == argc) usage();
  240.           strcpy(rule,argv[pt]);
  241.           pt++;
  242.           continue;
  243.           }
  244.        if (!strcmp(argv[pt], "-S"))
  245.        {
  246.           pt++;
  247.           if (pt == argc) usage();
  248.           strcpy(stop,argv[pt]);
  249.           pt++;
  250.           continue;
  251.           }
  252.        if (!strcmp(argv[pt], "-I"))
  253.        {
  254.           pt++;
  255.           if (pt == argc) usage();
  256.           strcpy(indexarr,argv[pt]);
  257.           pt++;
  258.           continue;
  259.           }
  260.        if (!strcmp(argv[pt], "-C"))
  261.        {
  262.           pt++;
  263.           if (pt == argc) usage();
  264.           cachesize = atoi(argv[pt]);
  265.           if (cachesize < 1) cachesize = 1;
  266.           pt++;
  267.           continue;
  268.           }
  269.        usage();
  270.        }
  271.  
  272.    initcache(cachesize);
  273.    initfile(dictionary);
  274.    initroot(rule);
  275.    initstop(stop);
  276.    
  277.    if (FAIL == readindex(indexarr))
  278.    {
  279.       buildindex();
  280.       writeindex(indexarr);
  281.       }
  282.  
  283.    /* check spelling for each file */
  284.    if (pt < argc)
  285.       while (pt < argc)
  286.       {
  287.           f = fopen(argv[pt],"rt");
  288.           if (f == NULL)
  289.           {
  290.               fprintf(stderr,"Unable to open file: %s\n",argv[pt]);
  291.               pt++;
  292.               continue;
  293.               }
  294.           checkspell(f);
  295.           fclose(f);
  296.           pt++;
  297.           }
  298.    else
  299.        checkspell(stdin);
  300.  
  301.    /* close down */
  302.    closefile();
  303.    fclose(fout);
  304.  
  305.    if (sortout)
  306.    {
  307.     /* We have made the wordlist now sort it using systems provided sort */
  308.     strcpy(ssys, sort);
  309.     strcat(ssys, " < ");
  310.     strcat(ssys, ftmp1);
  311.     strcat(ssys, " > ");
  312.     strcat(ssys